﻿Imports System.Reflection

<ComClass(ClsAcadObject.ClassId, ClsAcadObject.InterfaceId, ClsAcadObject.EventsId)>
Public Class ClsAcadObject

#Region "COM GUID"
    ' 这些 GUID 提供此类的 COM 标识 
    ' 及其 COM 接口。若更改它们，则现有的
    ' 客户端将不再能访问此类。
    Public Const ClassId As String = "d4c9dec8-8907-4eff-80ff-c5762bc3580f"
    Public Const InterfaceId As String = "4ead2c29-b28a-4e04-8421-acd3906a6884"
    Public Const EventsId As String = "b3f3fbd8-9809-4307-b757-112e3a804995"
#End Region

    ' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() 
    ' 否则， 将不会在 
    ' COM 注册表中注册此类，且无法通过
    ' CreateObject 创建此类。
    Public Sub New()
        MyBase.New()
    End Sub

    Private _ObjectName As String
    Public Property ObjectName() As String
        Get
            Return _ObjectName
        End Get
        Set(ByVal value As String)
            _ObjectName = value
        End Set
    End Property


    Private _ObjectID As Long
    Public Property ObjectID() As Long
        Get
            Return _ObjectID
        End Get
        Set(ByVal value As Long)
            _ObjectID = value
        End Set
    End Property

    Private _OwnerID As Long
    Public Property OwnerID() As Long
        Get
            Return _OwnerID
        End Get
        Set(ByVal value As Long)
            _OwnerID = value
        End Set
    End Property

    Private _Handle As String
    Public Property Handle() As String
        Get
            Return _Handle
        End Get
        Set(ByVal value As String)
            _Handle = value
        End Set
    End Property

    Public AcadObject As Object

    Public Sub CteateFromCad(acObj As Object)
        Me.AcadObject = acObj
        Dim t = Me.GetType()
        For Each item As PropertyInfo In t.GetProperties().ToList().Where(Function(c) c.CanWrite).ToList()
            Dim val = CallByName(acObj, item.Name, CallType.Get, Nothing)
            If val IsNot Nothing Then
                If val.GetType().IsCOMObject Then
                    item.SetValue(Me, val.Name, Nothing)
                Else
                    item.SetValue(Me, val, Nothing)
                End If
            End If
        Next
    End Sub
End Class


